import cv2

# https://github.com/opencv/opencv/tree/master/data/haarcascades
# opencv provide data

def face_img_detective():
    # train data
    # /Users/hunter/Anaconda/anaconda3/envs/CV/lib/python3.8/site-packages/cv2/data/haarcascade_frontalface_default.xml
    path = '/Users/hunter/Anaconda/anaconda3/envs/CV/lib/python3.8/site-packages/cv2/data/haarcascade_frontalface_default.xml'
    train_data = cv2.CascadeClassifier(path)
    # document https://docs.opencv.org/2.4.13.7/
    print("start.....")
    img = cv2.imread('img/test.jpg')

    # grey scaled
    gray_scaled_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # detect faces
    face_coordinates = train_data.detectMultiScale(gray_scaled_img)
    print(face_coordinates)

    # Draw rectangles around the faces
    # start at: up Left  then up Down
    for (x, y, w, h) in face_coordinates:
        cv2.rectangle(img, (x, y), (x+w, x+h), (0, 255, 0), 2)

    cv2.imshow("Iron Man", img)
    cv2.waitKey()
    print("finished")

def real_time_face_detctive():
    # train data
    path = '/Users/hunter/Anaconda/anaconda3/envs/CV/lib/python3.8/site-packages/cv2/data/haarcascade_frontalface_default.xml'
    train_data = cv2.CascadeClassifier(path)
    # document https://docs.opencv.org/2.4.13.7/
    print("start.....")
    #img = cv2.imread('img/test.jpg')
    webcam = cv2.VideoCapture(0)
    # webcam = cv2.VideoCapture('video_path')
    while True:
        successful_frame_read, frame = webcam.read()
        # grey scaled
        gray_scaled_img = cv2.cvtColor(frame,  cv2.COLOR_BGR2GRAY)
        # detect faces
        face_coordinates = train_data.detectMultiScale(gray_scaled_img)
        print(face_coordinates)

        # Draw rectangles around the faces
        # start at: up Left  then up Down
        for (x, y, w, h) in face_coordinates:
            cv2.rectangle(frame, (x, y), (x + w, x + h), (0, 255, 0), 2)

        cv2.imshow("Iron Man", frame)
        key = cv2.waitKey(1)
        # q for quit
        if key == 81 or key == 113:
            break
    print("finished")
    webcam.release()


if __name__ == '__main__':
    # face_img_detective()
    real_time_face_detctive()